# coding:utf-8
from django.shortcuts import render
from novel.getnovel import get_list, get_catalogue, get_detail, search
from django.http import HttpResponse
import json


# 小说类型页
def get_novel_page(request):
    return render(request, "./novel.html")


# 小说列表页
def get_novel_list(request):
    return render(request, "./novel_list.html", {'type': request.GET['type']})


# 小说数据爬取接口
def get_novel_data(request):
    if request.method == 'POST':
        what = request.POST.get('what')
        if what == 'list':
            return HttpResponse(json.dumps(get_list().biquge(request.POST.get('type'))))
        elif what == 'catalogue':
            return HttpResponse(json.dumps(get_catalogue().catalogue(
                request.POST.get('type'),
                request.POST.get('site')
            )))
        elif what == 'detail':
            return HttpResponse(json.dumps(get_detail().get_content(
                request.POST.get('site'),
                request.POST.get('chapter')
            )))
        elif what == 'search':
            return HttpResponse(json.dumps(search().get_content(
                request.POST.get('site'),
                request.POST.get('name')
            )))
    return None


# 小说内容页
def get_novel_detail(request):
    print(request.GET['chapter'])
    return render(
        request,
        "./novel_detail.html",
        {'chapter': {
            'chapter': request.GET['chapter'],
            'site': request.GET['site']
        }})


# 小说目录页
def get_novel_catalogue(request):
    return render(
        request,
        "./novel_catalogue.html",
        {'catalogue': {
            'catalogue': request.GET['catalogue'],
            'site': request.GET['site']
        }})


# 小说目录页
def get_novel_search(request):
    return render(request, "./search.html", {'search': {'website': request.GET['website'], 'name': request.GET['name']}})
